const merchantFacade = require('../facade/merchant.server.facade.js');
const api=require('../api/external.merchant.js');
var eventproxy = require('eventproxy');
const _ = require('lodash');
const envConfig = require('../../config/env/config_development');
var formidable = require('formidable');
var async=require('async');



var _f=require('../util/file.js');

module.exports = {
    regist: regist,
    login: login,
    update: update,
    logout: logout,
    updateByAdmin: updateByAdmin,
    updateRealInfo:updateRealInfo
}

var ep = new eventproxy();

ep.on('err', function (res, status, errMessage) {
    var data = {
        code: status,
        message: errMessage,
        data: null
    }
    res.json(data);
});

function regist(req, res, next) {


    var form = new formidable.IncomingForm();
    _f.uploadSetting(form);
    form.parse(req, function (err, fields, files) {

        var merchant = fields;

        merchantFacade.addMerchant(merchant, function (err, docs) {
            if (err) {
                console.error(err);
            }
            console.log(files);
            var fileName=docs._id+'.jpg';
            _f.rename(files.idcard.path,envConfig.upload.localIdcard+fileName);
            _f.rename(files.img.path,envConfig.upload.localImg+fileName);
            res.json(docs);
        })
    });


}

function login(req, res, next) {

    let merchant = req.body;
    merchantFacade.check(merchant.name, merchant.password, function (err, docs) {
        if (err) {
            res.json(500);
        }
        console.log(docs);
        if(docs==null){
            return res.json(404);
        }else{
            req.session.merchant=docs;
            return res.json(docs);
        }

    })

}

function logout(req, res, next) {

        console.log(req.session.merchant)
        req.session.merchant = null;
        res.json('200');


}


function updateByAdmin(req, res, next) {

    let body = req.body;
    console.log(body);
    let mid= body.mid;
    let block = body.block;
    let state = body.state;

    console.log(mid);
    merchantFacade.updateBlockState(mid,block,state, function (err, docs) {
        console.log(docs);
        if (err) {
            res.json(0);
        }
        res.json(1);
    })
};


function updateRealInfo(req, res, next) {

    let body = req.body;
    let mid= body.mid;
    let block = body.block;
    let state = body.state;
    let msg=body.msg;

    console.log(mid);
    merchantFacade.updateRealInfo(mid,block,state,msg,function (err, docs) {
        console.log(docs);
        if (err) {
            res.json(0);
        }
        res.json(1);
    })
};




function update(req, res, next) {


    var form = new formidable.IncomingForm();
    _f.uploadSetting(form);
    form.parse(req, function (err, fields, files) {

        var updateMerchant = fields;
        console.log(fields)
        console.log(files)
        merchantFacade.updateMerchant(updateMerchant, function (err, docs) {
            if (err) {
                console.error(err);
            }
            var fileName=docs._id+'.jpg';
            _f.rename(files.idcard.path,envConfig.upload.localIdcard+fileName);
            _f.rename(files.img.path,envConfig.upload.localImg+fileName);
            res.json(docs);
        })
    });


}

